package com.study_online.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @author 张天霸
 */

@Repository
public interface MyBaseMapper<T> extends BaseMapper<T> {

    /**
     * 点赞
     *
     * @param likeId
     * @param uid
     * @param addTime
     * @param tableName
     */
    @Insert("insert into ${tableName} values(#{likeId},#{uid},#{addTime}) ")
    void insertLike(@Param("likeId") Integer likeId, @Param("uid") Integer uid, @Param("addTime") String addTime, @Param("tableName") String tableName);

    /**
     * 取消点赞
     *
     * @param likeId
     * @param uid
     * @param addTime
     * @param tableName
     * @param tableField
     */
    @Delete("delete from ${tableName} where ${tableField} = #{likeId} and uid = #{uid}")
    void deleteLike(@Param("likeId") Integer likeId, @Param("uid") Integer uid, @Param("addTime") String addTime, @Param("tableName") String tableName, @Param("tableField") String tableField);

    /**
     * 点踩
     *
     * @param dislikeId
     * @param uid
     * @param addTime
     * @param tableName
     */
    @Insert("insert into ${tableName} values(#{dislikeId},#{uid},#{addTime})")
    void insertDislike(@Param("dislikeId") Integer dislikeId, @Param("uid") Integer uid, @Param("addTime") String addTime, @Param("tableName") String tableName);

    /**
     * 取消点踩
     *
     * @param dislikeId
     * @param uid
     * @param addTime
     * @param tableName
     * @param tableField
     */
    @Delete("delete from ${tableName} where ${tableField} = #{dislikeId} and uid = #{uid}")
    void deleteDislike(@Param("dislikeId") Integer dislikeId, @Param("uid") Integer uid, @Param("addTime") String addTime, @Param("tableName") String tableName, @Param("tableField") String tableField);

    /**
     * 收藏帖子
     *
     * @param collectedId
     * @param uid
     */
    @Insert("insert into `forum_collection` values(#{collectedId},#{uid},#{addTime},null)")
    void collect(@Param("collectedId") Integer collectedId, @Param("uid") Integer uid, @Param("addTime") String addTime);

    /**
     * 取消收藏帖子
     *
     * @param collectedId
     * @param uid
     */
    @Delete("delete from `forum_collection` where collected_id = #{collectedId} and uid = #{uid}")
    void deleteCollect(@Param("collectedId") Integer collectedId, @Param("uid") Integer uid);

    /**
     * 删除帖子（包含删除所有连带回帖、评论、收藏、点赞、点踩）
     * @param fpId
     * @return
     */
    @Delete("DELETE fp,fpl,fpd,cp,cpl,cpd,fc,fcl,fcd,fr,frl,frd FROM " +
            "forum_fatherpost AS fp " +
            "LEFT JOIN fatherpost_like AS fpl ON fp.fatherpostId = fpl.fatherpost_id " +
            "LEFT JOIN fatherpost_dislike AS fpd ON fp.fatherpostId = fpd.fatherpost_id " +
            "LEFT JOIN forum_childpost AS cp ON fp.fatherpostId = cp.fatherpost_id " +
            "LEFT JOIN childpost_like AS cpl ON cp.childpostId = cpl.childpost_id " +
            "LEFT JOIN childpost_dislike AS cpd ON cp.childpostId = cpd.childpost_id " +
            "LEFT JOIN forum_comments AS fc ON cp.childpostId = fc.childpost_id " +
            "LEFT JOIN comments_like AS fcl ON fc.commentId = fcl.comment_id " +
            "LEFT JOIN comments_dislike AS fcd ON fc.commentId = fcd.comment_id " +
            "LEFT JOIN forum_reply AS fr ON fc.commentId = fr.comment_id " +
            "LEFT JOIN reply_like AS frl ON fr.reply_id = frl.reply_id " +
            "LEFT JOIN reply_dislike AS frd ON fr.reply_id = frd.reply_id " +
            "WHERE fp.fatherpostId = #{fpId}")
    void deleteForum(Integer fpId);


}
